package com.xiaochong.tir.manager.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.ExchangeDimensionMiddle;
import com.xiaochong.tir.common.data.po.ExchangeTransactionpairMiddle;
import com.xiaochong.tir.common.data.po.TimeDimension;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.service.ExchangeDimensionMiddleService;
import com.xiaochong.tir.manager.util.ListSortUtil;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ReturnResult;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * Created by admin on 2018/6/6.
 * 时间维度管理
 */
@Controller
@RequestMapping(value = ManagerVersion.API_V1_PREFIX+"latmdle/")
@Api(value = "交易所-时间维度")
public class DimensionMiddleController {

    @Autowired
    private ExchangeDimensionMiddleService exchangeDimensionMiddleService;
    @Autowired
    private RedisServer redisServer;
    /**
     * 交易所下时间维度列表页
     * @param id
     * @param exchangeName
     * @param model
     * @return
     */
    @GetMapping("latmdlelist")
    public String dimensionMiddleList(@RequestParam("id")Integer id,
                                      @RequestParam("exchangeName")String exchangeName,
                                      Model model){
        model.addAttribute("id",id);
        model.addAttribute("exchangeName",exchangeName);
        return "/dimensionmiddle/latmdlepage";
    }

    /**
     * 查询交易所下时间维度列表
     * @param exchangeDimensionMiddle
     * @param pageSize
     * @param pageIndex
     * @return
     */
    @ApiOperation(value = "查询交易所下时间维度列表",notes = "查询交易所下时间维度列表")
    @PostMapping("sellatmidele")
    @ResponseBody
    public ReturnResult<ExchangeDimensionMiddle> selDimensionMiddle(ExchangeDimensionMiddle exchangeDimensionMiddle,
                                                                    @RequestParam("limit") Integer pageSize,
                                                                    @RequestParam("page")Integer pageIndex){
        PageInfo<ExchangeDimensionMiddle> list =
                exchangeDimensionMiddleService.selectByMiddleList(exchangeDimensionMiddle,pageSize,pageIndex);
        ReturnResult<ExchangeDimensionMiddle> result =
                new ReturnResult<ExchangeDimensionMiddle>(0,"成功",list.getList(),list.getTotal());
        return result;
    }

    /**
     * 新增时间维度页面
     * @return
     */
    @GetMapping("addelat")
    public String addDimensionMiddlePage(@RequestParam("id")Integer id,
                                         @RequestParam("exchangeName")String exchangeName, Model model){
        model.addAttribute("id",id);
        model.addAttribute("exchangeName",exchangeName);
        return "/dimensionmiddle/addelatpage";
    }

    /**
     * 新增时间维度方法
     * @param checkList
     * @param exchangeId
     * @return
     */
    @ApiOperation(value = "新增交易所下时间维度",notes = "中间表")
    @PostMapping("addexchangelat")
    @ResponseBody
    public ReturnResult<ExchangeDimensionMiddle> addExchangeDimensionMiddle(@RequestParam("checkList")String checkList,
                                                                            @RequestParam("exchangeId")Integer exchangeId){
        redisServer.set(RedisKeysUtils.LOGINNAME,"新增交易所时间维度");
        ExchangeDimensionMiddle exchangeDimensionMiddle = new ExchangeDimensionMiddle();
        List<TimeDimension> list = JSON.parseArray(checkList,TimeDimension.class);
        for (int i = 0; i < list.size(); i++) {
            TimeDimension timeDimension = list.get(i);
            exchangeDimensionMiddle.setTimeDimensionId(timeDimension.getId());
            exchangeDimensionMiddle.setDimensionName(timeDimension.getDimensionName());
            exchangeDimensionMiddle.setExchangeId(exchangeId);
            List<ExchangeDimensionMiddle> middles = exchangeDimensionMiddleService.selectByEntityList(exchangeDimensionMiddle);
            if(middles.size() > 0){
                if(middles.size() > 1){
                    ListSortUtil sortUtil = new ListSortUtil();
                    sortUtil.mySort(middles,"id","asc");
                    for (int j = 1; j < middles.size(); j++) {
                        ExchangeDimensionMiddle middle = middles.get(j);
                        exchangeDimensionMiddleService.deleteByPrimaryKeys(middle.getId());
                    }
                }
                continue;
            }
            exchangeDimensionMiddle.setEnableFlag(false);
            exchangeDimensionMiddle.setCreateTime(new Date());
            exchangeDimensionMiddleService.insertSelective(exchangeDimensionMiddle);
        }
        return new ReturnResult<ExchangeDimensionMiddle>(0,"成功",1,1);
    }

    /**
     * 查询交易所下没有的时间维度
     * @param timeDimension
     * @param pageSize
     * @param pageIndex
     * @return
     */
    @ApiOperation(value = "查询交易所下没有的时间维度",notes = "未关联交易所的时间维度")
    @PostMapping("selexchangelat")
    @ResponseBody
    public ReturnResult<TimeDimension> selTimeDimension(TimeDimension timeDimension,
                                                        @RequestParam("limit") Integer pageSize,
                                                        @RequestParam("page")Integer pageIndex){
        PageInfo<TimeDimension> list = exchangeDimensionMiddleService.selTimeDimension(timeDimension,pageSize,pageIndex);
        ReturnResult<TimeDimension> result= new ReturnResult<TimeDimension>(0,"成功",list.getList(),list.getTotal());
        return result;
    }

    /**
     * 启用停用
     * @param exchangeDimensionMiddle
     * @return
     */
    @ApiOperation(value = "修改交易所下时间维度状态",notes = "中间表")
    @PostMapping("enablelat")
    @ResponseBody
    public Object enableTimeDimension(ExchangeDimensionMiddle exchangeDimensionMiddle){
        redisServer.set(RedisKeysUtils.LOGINNAME,"时间维度启用/停用");
        ExchangeDimensionMiddle old =
        exchangeDimensionMiddleService.selectByPrimaryKey(exchangeDimensionMiddle.getId());
        exchangeDimensionMiddle.setUpdateTime(new Date());
        if(exchangeDimensionMiddle.getEnableFlag()){
            exchangeDimensionMiddle.setEnableFlag(false);
        }else {
            exchangeDimensionMiddle.setEnableFlag(true);
        }
        exchangeDimensionMiddleService.updateByPrimaryKey(exchangeDimensionMiddle,old);
        return new ReturnResult<ExchangeDimensionMiddle>(0,"成功",1,1);
    }

    /**
     * 删除时间维度
     * @param id
     * @return
     */
    @ApiOperation(value = "删除易所下时间维度",notes = "中间表")
    @PostMapping("deleteLelat")
    @ResponseBody
    public Object deleteTimeDimension(@RequestParam("id")Integer id){
        redisServer.set(RedisKeysUtils.LOGINNAME,"删除时间维度");
        ExchangeDimensionMiddle old =
        exchangeDimensionMiddleService.selectByPrimaryKey(id);
        exchangeDimensionMiddleService.deleteByPrimaryKey(id,old);
        return new ReturnResult<ExchangeDimensionMiddle>(0,"成功",1,1);
    }
}
